package cn.iocoder.yudao.module.pplan.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.distm.dal.dataobject.schedule.ScheduleRecordDO;
import cn.iocoder.yudao.module.distm.dal.mysql.schedule.ScheduleRecordHistoryMapper;
import cn.iocoder.yudao.module.distm.dal.mysql.schedule.ScheduleRecordMapper;
import cn.iocoder.yudao.module.distm.service.schedule.ScheduleRecordService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class SyncDistmSchedeuleStatusJob implements JobHandler {

    @Resource
    private ScheduleRecordMapper scheduleRecordMapper;

    @Resource
    private ScheduleRecordHistoryMapper scheduleRecordHistoryMapper;

    @Resource
    private ScheduleRecordService scheduleRecordService;

    @Override
    @TenantIgnore // 忽略多租户
    public String execute(String param) {

        // 查询调度数据--此处目前考虑数据量比较少，未考虑分页
        List<ScheduleRecordDO> scheduleRecordDOS = scheduleRecordMapper.selectList();
        List<ScheduleRecordDO> updateDos = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(scheduleRecordDOS)){
            for (ScheduleRecordDO scheduleRecordDO : scheduleRecordDOS) {
                if (isWithinNextThreeHours(scheduleRecordDO.getDepTime()) && scheduleRecordDO.getMarkStatus()==0){
                    // 航机任务确认   起飞前 三个小时
                    if (scheduleRecordDO.getPstatus()<4){
                        updateDos.add(new ScheduleRecordDO().setId(scheduleRecordDO.getId()).setMarkStatus(1));
                    }
                }
                if (isWithinNextTwoHours(scheduleRecordDO.getDepTime()) && scheduleRecordDO.getMarkStatus()==0){
                    // 发车确认（起飞前两个小时）
                    if (scheduleRecordDO.getPstatus()<7){
                        updateDos.add(new ScheduleRecordDO().setId(scheduleRecordDO.getId()).setMarkStatus(1));
                    }
                }
            }
        }
        if (CollectionUtil.isNotEmpty(updateDos)) {
            scheduleRecordMapper.updateBatch(CollectionUtil.distinct(updateDos, ScheduleRecordDO::getId, true));
        }
        return "提醒任务执行成功";
    }

    private static boolean isWithinNextThreeHours(LocalTime targetTime) {
        LocalDateTime now = LocalDateTime.now();

        // 组合成今天的这个时间
        LocalDateTime targetDateTimeToday = LocalDateTime.of(now.toLocalDate(), targetTime);

        // 如果今天这个时间已经过去了，就考虑明天
        LocalDateTime futureTargetDateTime = targetDateTimeToday;
        if (futureTargetDateTime.isBefore(now)) {
            futureTargetDateTime = futureTargetDateTime.plusDays(1);
        }

        // 计算时间差
        Duration duration = Duration.between(now, futureTargetDateTime);
        return !duration.isNegative() && duration.toHours() <= 3;
    }

    private static boolean isWithinNextTwoHours(LocalTime targetTime) {
        LocalDateTime now = LocalDateTime.now();

        // 组合成今天的这个时间
        LocalDateTime targetDateTimeToday = LocalDateTime.of(now.toLocalDate(), targetTime);

        // 如果今天这个时间已经过去了，就考虑明天
        LocalDateTime futureTargetDateTime = targetDateTimeToday;
        if (futureTargetDateTime.isBefore(now)) {
            futureTargetDateTime = futureTargetDateTime.plusDays(1);
        }

        // 计算时间差
        Duration duration = Duration.between(now, futureTargetDateTime);
        return !duration.isNegative() && duration.toHours() <= 2;
    }


}
